from manimlib import *


map = {
    "d": ORANGE,
    "k": GREY_B,
    r"\alpha": RED_B,
    r"\beta": YELLOW_B,
    r"\gamma": TEAL_B,
    r"\delta": PURPLE_B,
    r"\epsilon": PINK,

}


def rotate_point_around(point, about, angle):
    mat = rotation_matrix_transpose(angle, OUT)
    return np.dot(point-about, mat)+about


def get_angle(center, outside):
    vec = normalize(outside-center)
    return math.atan2(vec[1], vec[0])


def get_angle_label(angle, buff):
    center = angle.arc_center
    p = angle.pfp(0.5)
    vec = normalize(p-center)
    return p+vec*buff


class moly(Polygon):

    def __init__(self, vertices=None, **kwargs):
        if vertices == None:
            super().__init__(np.array([1, 2, 0]), np.array(
                [-2, -1, 0]), np.array([2, -1, 0]), color=BLUE, fill_opacity=0.5)
        else:
            super().__init__(*vertices, color=BLUE, fill_opacity=0.5)

    def get_side_length(self):
        "三角形边长"
        A, B, C = Polygon.get_vertices(self)
        self.rotate
        a = math.sqrt((B[0]-C[0])**2 + (B[1]-C[1])**2)
        b = math.sqrt((A[0]-C[0])**2 + (A[1]-C[1])**2)
        c = math.sqrt((A[0]-B[0])**2 + (A[1]-B[1])**2)

        return a, b, c

    def get_three_angle(self):
        "三个角"
        a, b, c = self.get_side_length()
        AngleA = math.acos((b**2 + c**2 - a**2) / (2 * b * c))
        AngleB = math.acos((a**2 + c**2 - b**2) / (2 * a * c))
        AngleC = math.acos((a**2 + b**2 - c**2) / (2 * a * b))
        return AngleA, AngleB, AngleC

    def get_equilateral_vertices(self):
        "等边三角形三点"
        A, B, C = Polygon.get_vertices(self)
        a, b, c = self.get_three_angle()

        D = find_intersection(B, normalize(rotate_point_around(
            C, B, b/3)-B), C, normalize(rotate_point_around(B, C, -c/3)-C))
        E = find_intersection(A, normalize(rotate_point_around(
            C, A, -a/3)-A), C, normalize(rotate_point_around(A, C, c/3)-C))
        F = find_intersection(B, normalize(rotate_point_around(
            A, B, -b/3)-B), A, normalize(rotate_point_around(B, A, a/3)-A))
        return [np.array([x[0][0], x[0][1], 0]) for x in [D, E, F]]

    def get_equilateral(self):
        "获得正三角形"
        return Polygon(*self.get_equilateral_vertices(), color=ORANGE, fill_opacity=0.5)

    def get_trisector(self):
        "获得内角三等分线"
        D, E, F = (i for i in self.get_equilateral_vertices())
        A, B, C = self.get_vertices()

        return VGroup(Line(A, E), Line(A, F), Line(B, F), Line(B, D), Line(C, D), Line(C, E))

    def get_angles(self):
        "获得三等分角"
        vg = VGroup()
        points = self.get_vertices()
        equ = self.get_equilateral_vertices()
        color = [RED_B, YELLOW_B, TEAL_B]
        for i in range(3):
            arc1 = Arc(radius=0.5, stroke_width=2, arc_center=points[i], start_angle=get_angle(points[i], points[(
                i+2) % 3]), angle=-get_angle(points[i], points[(i+2) % 3])+get_angle(points[i], equ[(i+1) % 3]))
            arc2 = Arc(radius=0.5, stroke_width=2, arc_center=points[i], start_angle=get_angle(points[i], equ[(
                i+1) % 3]), angle=-get_angle(points[i], equ[(i+1) % 3])+get_angle(points[i], equ[(i+2) % 3]))
            arc3 = Arc(radius=0.5, stroke_width=2, arc_center=points[i], start_angle=get_angle(points[i], equ[(
                i+2) % 3]), angle=-get_angle(points[i], equ[(i+2) % 3])+get_angle(points[i], points[(i+1) % 3]))
            vg.add(VGroup(arc1, arc2, arc3).set_color(color[i]))
        return vg

    def get_angle_labels(self, buff=0.1):
        "获得三等分角标签"
        vg = VGroup()
        label = [r"\alpha", r"\beta", r"\gamma"]
        arcs = self.get_angles()
        color = [RED_B, YELLOW_B, TEAL_B]
        for i in range(3):
            a = Tex(label[i]).scale(.5).move_to(
                get_angle_label(arcs[i][0], buff)).set_color(color[i])
            b = Tex(label[i]).scale(.5).move_to(
                get_angle_label(arcs[i][1], buff)).set_color(color[i])
            c = Tex(label[i]).scale(.5).move_to(
                get_angle_label(arcs[i][2], buff)).set_color(color[i])
            vg.add(VGroup(a, b, c))
        return vg

    def get_big_angles(self):
        points = self.get_vertices()
        angles = self.get_three_angle()
        color = [RED_B, YELLOW_B, TEAL_B]
        vg = VGroup(*[Arc(radius=.25, stroke_width=2, arc_center=points[i], start_angle=get_angle(
            points[i], points[(i+1) % 3]), angle=angles[i]).set_color(color[i]) for i in range(3)])
        return vg

    def get_circumcenter(self):
        "外心坐标"
        A, B, C = Polygon.get_vertices(self)
        AngleA, AngleB, AngleC = self.get_three_angle()
        Ax, Ay = A[0], A[1]
        Bx, By = B[0], B[1]
        Cx, Cy = C[0], C[1]

        xup = Ax * math.sin(2 * AngleA) + Bx * \
            math.sin(2 * AngleB) + Cx * math.sin(2 * AngleC)
        yup = Ay * math.sin(2 * AngleA) + By * \
            math.sin(2 * AngleB) + Cy * math.sin(2 * AngleC)
        down = math.sin(2 * AngleA) + math.sin(2 * AngleB) + \
            math.sin(2 * AngleC)
        return np.array([xup/down, yup/down, 0])

    def get_circumcircle(self):
        "外接圆"
        A, B, C = Polygon.get_vertices(self)
        Ax, Ay = A[0], A[1]
        circumcenter = self.get_circumcenter()
        circumradius = math.sqrt(
            (Ax-circumcenter[0])**2 + (Ay-circumcenter[1])**2)
        return Circle(radius=circumradius).move_to(circumcenter)


class SineRule(Scene):
    def construct(self):
        tri = moly()
        A = Tex("A").next_to(tri.get_vertices()[0], UP)
        B = Tex("B").next_to(tri.get_vertices()[
            1], DL, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER/2**0.5)
        C = Tex("C").next_to(tri.get_vertices()[
            2], DR, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER/2**0.5)
        vg = VGroup(A, B, C)
        line_BC = Line(*tri.get_vertices()[1:])
        line_BC_label = Tex("a").next_to(line_BC, DOWN)
        circle = tri.get_circumcircle()
        diamiter = Line(circle.get_left(), circle.get_right())
        d = Tex("d").next_to(diamiter, UP)
        diamiter_with_label = VGroup(diamiter, d)
        self.play(ShowCreation(VGroup(tri)))
        self.wait()
        self.play(Write(vg))
        self.wait()
        self.play(ShowCreation(VGroup(circle)))
        self.wait()
        self.play(ShowCreation(diamiter), Write(d))

        Rtri = moly()
        A_angle = tri.get_big_angles()[0]
        A1_angle = A_angle.copy()
        angle = get_angle(tri.get_circumcenter(), 2*tri.get_circumcenter() -
                          tri.get_vertices()[1])-get_angle(tri.get_circumcenter(), tri.get_vertices()[0])
        A1_dot = Dot(Rtri.get_vertices()[0])
        A1_dot.set_opacity(0)
        self.wait()
        self.play(ShowCreation(VGroup(A_angle)))
        self.wait()
        self.add(Rtri, A1_dot, diamiter_with_label)
        self.bring_to_back(A1_dot)

        self.play(
            Rotating(A1_dot, angle, about_point=tri.get_circumcenter(),
                     rate_func=smooth),
            UpdateFromAlphaFunc(Rtri, lambda m, alpha: m.become(moly(
                [A1_dot.get_center(), *tri.get_vertices()[1:]]).set_fill(opacity=0.5*alpha))),
            UpdateFromFunc(A1_angle, lambda m: m.become(
                Rtri.get_big_angles()[0])),
            run_time=1
        )

        A1 = Tex("A'").next_to(Rtri.get_vertices()[0])
        self.play(Write(A1))
        right_angle = Elbow(
            angle=PI/2, stroke_width=2).next_to(Rtri.get_vertices()[2], UL, buff=0)
        self.wait()
        self.play(ShowCreation(VGroup(right_angle)))
        self.wait()
        self.play(Rotating(diamiter_with_label, angle=get_angle(tri.get_circumcenter(), Rtri.get_vertices()[
                  0]), about_point=circle.get_center(), rate_func=smooth), ShowCreation(VGroup(line_BC)), Write(line_BC_label), run_time=1)
        self.wait()
        formula = Tex("\sin", " A'", "=", "{", "a", "\\over", " d", "}").next_to(
            circle, UP, buff=0.5)
        self.play(TransformFromCopy(A1, formula[1]), TransformFromCopy(d, formula[-1]), TransformFromCopy(
            line_BC_label, formula[-3]), Write(formula[0]), Write(formula[2]), Write(formula[-2]))
        self.wait()
        formula2 = Tex("\sin", " A", "=", "{", "a", "\\over", " d", "}").next_to(
            circle, UP, buff=0.5)
        self.play(Indicate(A_angle, scale_factor=2),
                  Indicate(A1_angle, scale_factor=2))
        self.wait()
        self.play(ReplacementTransform(formula, formula2))
        self.wait()
        formula3 = Tex(" d", "=", "{", "a", "\\over", "\sin", " A", "}").next_to(
            circle, UP, buff=0.5)
        self.play(TransformMatchingTex(formula2, formula3))
        self.wait()
        same = TexText("同理").to_edge(DOWN)
        self.play(FadeOut(VGroup(Rtri, A_angle, A1_angle, line_BC, right_angle, A1)), Write(same), Rotating(diamiter_with_label, angle=-
                  get_angle(tri.get_circumcenter(), Rtri.get_vertices()[0]), about_point=circle.get_center(), rate_func=smooth, run_time=1))
        self.wait()
        line_AB_label = Tex("c").next_to(sum(tri.get_vertices()[:-1])/2, LEFT)
        line_CA_label = Tex("b").next_to(
            (tri.get_vertices()[0]+tri.get_vertices()[2])/2)
        self.play(Write(line_AB_label), Write(line_CA_label))
        self.wait()
        formula4 = Tex("d", "=", "{", "a", "\\over", "\sin", " A", "}", "=", "{", "b", "\\over",
                       "\sin", " B", "}", "=", "{", "c", "\\over", "\sin", " C", "}").next_to(circle, UP, buff=0.5)
        self.play(ReplacementTransform(formula3, formula4[:len(formula3)]), *[Write(formula4[i]) for i in [6, 8, 9, 11, 13, 14]], *[
                  TransformFromCopy(i, formula4[j]) for i, j in [(line_CA_label, 7), (B, 10), (line_AB_label, 12), (C, 15)]])
        self.wait(.5)
        self.play(FadeOut(same))
        self.wait(59/60)
        formula4_for_Trans = Tex(
            "d={a\\over\sin A}={b\\over\sin B}={c\\over\sin C}").next_to(circle, UP, buff=0.5)
        self.play(FadeOut(formula4), FadeIn(formula4_for_Trans), run_time=1/60)
        conclu = TexText(
            "正弦定理:", "$d={a\\over\sin A}={b\\over\sin B}={c\\over\sin C}$").to_edge(DOWN)
        self.play(ReplacementTransform(
            formula4_for_Trans[0], conclu[-1]), Write(conclu[0]))
        self.wait(3)
        self.play(VGroup(*self.mobjects).animate.scale(0))


class TrigFunctions(Scene):
    def construct(self):
        euler_formula = Tex(r"e^{i\theta}=", "\cos", r"(\theta)+i",
                            "\sin", r"(\theta)", isolate=[r"\theta", "(", ")", "+"])
        self.play(GrowFromCenter(euler_formula))
        self.wait()
        self.play(euler_formula.animate.to_edge(UP))
        self.wait()
        alpha = Tex(r"e^{i\alpha}=", "\cos", r"(\alpha)+i", "\sin", r"(\alpha)",
                    isolate=[r"\alpha", r"\beta", "(", ")", "+"]).shift(UP*0.5)
        beta = Tex(r"e^{i\beta}=", "\cos", r"(\beta)+i", "\sin", r"(\beta)",
                   isolate=[r"\alpha", r"\beta", "(", ")", "+"]).shift(DOWN*0.5)
        self.play(
            ReplacementTransform(euler_formula.copy(), alpha),
            ReplacementTransform(euler_formula.copy(), beta)
        )
        alphaplusbeta = Tex(r"e^{i\alpha}", "\\cdot", r" e^{i\beta}", "=", "\left[", "\cos", r"(\alpha)+i", "\sin", r"(\alpha)", r"\right]",
                            "\cdot", "\left[", "\cos", r"(\beta)+i", "\sin", r"(\beta)", r"\right]", isolate=[r"\alpha", r"\beta", "(", ")", "+"])
        self.wait()
        self.play(
            ReplacementTransform(alpha[:2], alphaplusbeta[:2]),
            ReplacementTransform(beta[:2], alphaplusbeta[3:5]),
            Write(alphaplusbeta[2]))
        self.play(
            ReplacementTransform(alpha[2], alphaplusbeta[5]),
            ReplacementTransform(beta[2], alphaplusbeta[5])
        )
        self.play(
            *[Write(alphaplusbeta[i]) for i in [6, 17, 18, 19, 30]],
            ReplacementTransform(alpha[3:], alphaplusbeta[7:17]),
            ReplacementTransform(beta[3:], alphaplusbeta[20:-1])
        )
        self.wait()
        alphaplusbeta2 = Tex(r"e^{i(\alpha+\beta)}", "=", "\left[", "\cos", r"(\alpha)+i", "\sin", r"(\alpha)", r"\right]",
                             "\cdot", "\left[", "\cos", r"(\beta)+i", "\sin", r"(\beta)", r"\right]", isolate=[r"\alpha", r"\beta", "(", ")", "+"])
        self.play(TransformMatchingTex(alphaplusbeta, alphaplusbeta2))
        self.wait()
        alphaplusbeta3 = Tex(r"\cos", r"(\alpha+\beta)", "+i", "\sin", r"(\alpha+\beta)", "=",
                             "\left[", "\cos", r"(\alpha)+i", "\sin", r"(\alpha)", r"\right]", "\cdot", "\left[", "\cos", r"(\beta)+i", "\sin", r"(\beta)", r"\right]", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).set_width(14)
        self.play(FlashAround(euler_formula))
        self.wait()
        self.play(
            ReplacementTransform(alphaplusbeta2[6:], alphaplusbeta3[14:]),
            FadeTransform(alphaplusbeta2[:6], alphaplusbeta3[:14])
        )
        self.wait()
        alphaplusbeta4 = Tex(r"\cos", r"(\alpha+\beta)", "+i", "\sin", r"(\alpha+\beta)", "=", "\cos", r"(\alpha)", "\cos", r"(\beta)", "+i", "\sin", r"(\alpha)", "\cos",
                             r"(\beta)", "+i", "\cos", r"(\alpha)", "\sin", r"(\beta)", "-", "\sin", r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).set_width(14)
        self.play(
            ReplacementTransform(alphaplusbeta3[:15], alphaplusbeta4[:15]),
            TransformMatchingTex(alphaplusbeta3[15:], alphaplusbeta4[15:])
        )
        self.wait()
        alphaplusbeta5 = Tex(r"\cos", r"(\alpha+\beta)", "+i", "\sin", r"(\alpha+\beta)", "=", "\left[", "\cos", r"(\alpha)", "\cos", r"(\beta)", "-", "\sin", r"(\alpha)", "\sin", r"(\beta)", r"\right]",
                             "+i", "\left[", "\sin", r"(\alpha)", "\cos", r"(\beta)", "+", "\cos", r"(\alpha)", "\sin", r"(\beta)", r"\right]", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).set_width(14)
        self.play(
            ReplacementTransform(alphaplusbeta4[:15], alphaplusbeta5[:15]),
            TransformMatchingTex(alphaplusbeta4[15:], alphaplusbeta5[15:])
        )
        self.wait()
        Sine = Tex("\sin", r"(\alpha+\beta)", "=", "\sin", r"(\alpha)", "\cos", r"(\beta)", "+", "\cos",
                   r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).move_to(UP*0.5)
        Cosine = Tex("\cos", r"(\alpha+\beta)", "=", "\cos", r"(\alpha)", "\cos", r"(\beta)", "-", "\sin",
                     r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).move_to(DOWN*0.5)
        self.play(
            ReplacementTransform(alphaplusbeta5[:6], Cosine[:6]),
            ReplacementTransform(alphaplusbeta5[8:14], Sine[:6]),
            ReplacementTransform(alphaplusbeta5[16:33], Cosine[7:]),
            ReplacementTransform(alphaplusbeta5[37:-1], Sine[7:]),
            ReplacementTransform(alphaplusbeta5[14].copy(), Cosine[6]),
            ReplacementTransform(alphaplusbeta5[14], Sine[6]),
            *[FadeOut(alphaplusbeta5[i])
              for i in [6, 7, 15, 33, 34, 35, 36, -1]],
            FadeOut(euler_formula)
        )
        self.wait(2)
        SineMinus = Tex("\sin", r"(\alpha-\beta)", "=", "\sin", r"(\alpha)", "\cos", r"(\beta)", "-", "\cos",
                        r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "-", "+"]).move_to(UP*0.5)
        CosineMinus = Tex("\cos", r"(\alpha-\beta)", "=", "\cos", r"(\alpha)", "\cos", r"(\beta)", "+", "\sin",
                          r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).move_to(DOWN*0.5)
        self.play(
            ReplacementTransform(Sine, SineMinus),
            ReplacementTransform(Cosine, CosineMinus)
        )
        self.wait(2)
        self.play(VGroup(SineMinus, CosineMinus).animate.scale(20))


class intro(Scene):
    def construct(self):
        tri = moly().scale(1.5)
        lines = tri.get_trisector()
        center = tri.get_equilateral()
        angle = tri.get_angles()
        angle_label = tri.get_angle_labels()
        self.play(ShowCreation(VGroup(tri)))
        self.wait()
        A = Tex("A").next_to(tri.get_vertices()[0], UP)
        B = Tex("B").next_to(tri.get_vertices()[1], DL)
        C = Tex("C").next_to(tri.get_vertices()[2], DR)

        vg = VGroup(A, B, C)
        self.play(Write(vg))
        self.wait()
        for i in range(3):
            self.play(ShowCreation(lines[i*2]), ShowCreation(lines[i*2+1]))
            self.play(*[ShowCreation(a) for a in angle[i]])
            self.play(Write(angle_label[i]))
            self.wait()
        self.play(ShowCreation(VGroup(center)))

        DEF = VGroup(*[Tex(tex).move_to(tri.get_equilateral_vertices()[i]*1.8-sum(
            tri.get_equilateral_vertices())*0.8/3) for i, tex in enumerate(["D", "E", "F"])])
        self.wait()
        self.play(Write(DEF))
        self.wait()
        angle_sum = Tex(r"3\alpha+3\beta+3\gamma=180^\circ",
                        isolate=[r"\alpha", r"\beta", r"\gamma", "+", "="]).next_to(tri, DOWN)
        angle_sum.set_color_by_tex_to_color_map(map)
        self.play(
            *[ReplacementTransform(a.copy(), angle_sum[3*i+1])
              for i in range(3) for a in angle_label[i]],
            *[Write(angle_sum[i]) for i in range(len(angle_sum)) if not i in [3*x+1 for x in range(3)]]
        )
        self.wait()
        angle_sum2 = Tex(r"\alpha+\beta+\gamma=60^\circ",
                         isolate=[r"\alpha", r"\beta", r"\gamma", "="]).next_to(tri, DOWN)
        angle_sum2.set_color_by_tex_to_color_map(map)
        self.play(
            TransformMatchingTex(
                angle_sum, angle_sum2,
                transform_mismatches=True,
            )
        )
        self.wait()
        self.play(angle_sum2.animate.scale(.5).to_corner(UL))
        self.wait()
        text1 = TexText(r"我们对$\triangle ABC$使用正弦定理").to_edge(DOWN)
        c = Tex("c").next_to(sum(tri.get_vertices()[:-1])/2, LEFT)
        sinerule1 = Tex("d", "=", "{c", "\\over", r"\sin", r"(3\gamma)}", isolate=[
                        r"\gamma", "(", ")"]).set_color_by_tex_to_color_map(map).next_to(tri, DOWN)
        self.play(Write(text1), Write(c))
        self.wait()
        self.play(
            ReplacementTransform(c.copy(), sinerule1[2]),
            *[ReplacementTransform(gamma.copy(), sinerule1[7])
              for gamma in angle_label[2]],
            *[Write(sinerule1[i])
              for i in range(len(sinerule1)) if i != 2 and i != 7],
        )
        text1_with_note = TexText(
            r"我们对$\triangle ABC$使用正弦定理", r"\\$d$为$\triangle ABC$外接圆直径")
        text1_with_note[1].scale(0.5).set_color(GREY)
        text1_with_note.to_edge(DOWN, buff=0.1)
        self.play(ReplacementTransform(text1[0], text1_with_note[0]), FadeIn(
            text1_with_note[1], shift=UP))
        self.wait()
        sinerule2 = Tex("c", "=", "d", "\sin", "(3\gamma)", isolate=[
                        r"\gamma", "(", ")"]).set_color_by_tex_to_color_map(map).next_to(tri, DOWN)
        self.play(TransformMatchingTex(sinerule1, sinerule2))
        self.wait()
        self.play(sinerule2.animate.scale(.5).next_to(
            angle_sum2, DOWN).align_to(angle_sum2, LEFT))
        self.play(text1_with_note[1].animate.next_to(
            sinerule2), FadeOut(text1_with_note[0]))
        self.wait()
        sinerule2.fix_in_frame()
        angle_sum2.fix_in_frame()
        note = text1_with_note.fix_in_frame()
        frame = self.camera.frame
        text2 = TexText(r"接下来我们对$\triangle ABF$进行考察").to_edge(
            DOWN).fix_in_frame()
        self.play(Write(text2))
        self.wait()
        ABF = Polygon(
            *tri.get_vertices()[:-1], tri.get_equilateral_vertices()[-1], color=BLUE, fill_opacity=0.5)
        to_move = VGroup(ABF, angle[0][2].copy(), angle[1][0].copy(
        ), angle_label[0][2].copy(), angle_label[1][0].copy(), c.copy()).fix_in_frame()
        for obj in to_move:
            self.add(obj)
        self.play(LaggedStart(FadeIn(to_move),
                  frame.animate.shift(DOWN*8), lag_ratio=.5))
        self.wait()
        self.play(FadeOut(text2))


class calcuAF(Scene):
    def construct(self):
        def transform_parts(range1, range2, tex1, tex2, transtype=FadeTransform):
            return AnimationGroup(transtype(tex1[range1[0]:range1[1]+1], tex2[range2[0]:range2[1]+1]),
                                  ReplacementTransform(tex1[0:range1[0]], tex2[0:range2[0]]), ReplacementTransform(tex1[range1[1]+1:], tex2[range2[1]+1:]))
        gap = 2
        ABF = moly([np.array([1.5, 2.75, 0]), np.array(
            [-3, -1.75, 0]), np.array([0.37063826, 0.1960389, 0])])
        alpha = ABF.get_big_angles()[0]
        alpha_label = Tex(r"\alpha").set_color(
            RED_B).scale(.5).move_to(get_angle_label(alpha, .1))
        beta = ABF.get_big_angles()[1]
        beta_label = Tex(r"\beta").set_color(
            YELLOW_B).scale(.5).move_to(get_angle_label(beta, .1))
        c = Tex("c").next_to(sum(ABF.get_vertices()[:-1])/2, LEFT)
        angle_sum = Tex(r"\alpha+\beta+\gamma=60^\circ", isolate=[
                        r"\alpha", r"\beta", r"\gamma", "="]).set_color_by_tex_to_color_map(map).scale(.5).to_corner(UL)
        c_value = Tex("c", "=", "d", "\sin", "(3\gamma)", isolate=[r"\gamma", "(", ")"]).scale(
            .5).set_color_by_tex_to_color_map(map).next_to(angle_sum, DOWN).align_to(angle_sum, LEFT)
        note = TexText(r"$d$为$\triangle ABC$外接圆直径").set_color(
            GREY).scale(.5).next_to(c_value)
        self.add(ABF, alpha, beta, alpha_label,
                 beta_label, c, angle_sum, c_value, note)
        text1 = TexText("设这条边为$x$").to_edge(DOWN)
        x = Tex("x").next_to(
            (ABF.get_vertices()[-1]+ABF.get_vertices()[0])/2, DR)
        self.wait()
        self.play(Write(text1))
        self.wait(.5)
        self.play(Write(x))
        self.wait()
        anglealphabeta = ABF.get_big_angles()[2].set_color([RED_B, YELLOW_B])
        anglealphabeta.scale(.5, about_point=anglealphabeta.arc_center)
        alphabetalabel = Tex(r"180^\circ", "-", r"\alpha", "-", r"\beta")\
            .set_color_by_tex_to_color_map(map).scale(.5)\
            .move_to(get_angle_label(anglealphabeta, .2))\
            .rotate(get_angle(anglealphabeta.arc_center, anglealphabeta.pfp(.5))-PI/2)
        self.play(
            ShowCreation(VGroup(anglealphabeta)),
            *[Write(alphabetalabel[i])
              for i in range(len(alphabetalabel)) if i != 2 and i != 4],
            ReplacementTransform(alpha_label.copy(), alphabetalabel[2]),
            ReplacementTransform(beta_label.copy(), alphabetalabel[4])
        )
        self.wait(gap)
        text2 = TexText("由正弦定理我们可以知道").to_edge(DOWN)
        find_x = Tex(r"{x", r"\over", "\sin", r"(\beta)}", "=", "{c", "\over", "\sin", r"(180^\circ-\alpha-\beta)", isolate=[
                     r"\alpha", r"\beta", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(Write(text2), FadeOut(text1))
        self.wait(gap)
        self.play(
            ReplacementTransform(x.copy(), find_x[0]),
            ReplacementTransform(beta_label.copy(), find_x[4]),
            ReplacementTransform(c.copy(), find_x[7]),
            ReplacementTransform(alphabetalabel.copy(), find_x[11:16]),
            *[Write(m) for i, m in enumerate(find_x) if not i in [0, 4, 7, 11, 12, 13, 14, 15]]
        )
        find_x2 = Tex(r"{x", r"\over", "\sin", r"(\beta)}", "=", "{c", "\over", "\sin", r"(\alpha+\beta)", isolate=[
                      r"\alpha", r"\beta", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.wait(gap)
        self.play(transform_parts([-6, -2], [-4, -2], find_x, find_x2))
        self.wait(gap)
        find_x3 = Tex(r"{x", "=", "{c", "\sin", r"(\beta)", "\over", "\sin", r"(\alpha+\beta)", isolate=[
                      r"\alpha", r"\beta", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(TransformMatchingTex(find_x2, find_x3))
        self.wait(gap)
        Sine = Tex("\sin", r"(\alpha+\beta)", "=", "\sin", r"(\alpha)", "\cos", r"(\beta)", "+", "\cos",
                   r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "+"]).scale(.5).to_corner(UR).set_color_by_tex_to_color_map(map)
        SineMinus = Tex("\sin", r"(\alpha-\beta)", "=", "\sin", r"(\alpha)", "\cos", r"(\beta)", "-", "\cos",
                        r"(\alpha)", "\sin", r"(\beta)", isolate=[r"\alpha", r"\beta", "(", ")", "-", "+"]).scale(.5).next_to(Sine, DOWN).align_to(Sine, RIGHT).set_color_by_tex_to_color_map(map)
        self.play(FadeIn(Sine), FadeIn(SineMinus), FadeOut(text2))
        self.wait()
        find_x4 = Tex(r"x", "=", "{c", "\sin", r"(\beta)", "\over", "\sin", r"(60^\circ-\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(FlashAround(angle_sum))
        self.wait()
        self.play(transform_parts([-4, -2], [-4, -2], find_x3, find_x4))
        find_x5 = Tex(r"x", "=", "{c", "\sin", r"(\beta)", "\over", "\sin", r"(60^\circ)", "\cos", "(\gamma)", "-", "\cos", "(60^\circ)", "\sin", "(\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.wait(gap)
        self.play(FlashAround(SineMinus))
        self.wait()
        self.play(TransformMatchingTex(find_x4[8:], find_x5[8:]), ReplacementTransform(
            find_x4[:8], find_x5[:8]))
        self.wait(gap)
        find_x6 = Tex(r"x", "=", "{c", "\sin", r"(\beta)", "\over", r"{\sqrt{3}", "\over", "2}", "\cos", "(\gamma)", "-", "{1", "\\over", " 2}", "\sin", "(\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(ReplacementTransform(find_x5[8:], find_x6[8:]), ReplacementTransform(
            find_x5[:8], find_x6[:8]))
        self.wait(gap)
        find_x7 = Tex(r"x", "=", "{", "2", "c", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(TransformMatchingTex(find_x6, find_x7))
        self.wait(gap)
        find_x8 = Tex(r"x", "=", "{", "2", "d", "\sin", "(3\gamma)", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(FlashAround(c_value))
        self.wait()
        self.play(transform_parts([3, 3], [3, 8], find_x7, find_x8))
        self.wait(gap)
        text3 = TexText("我们把$\sin(3\gamma)$拆开").to_edge(DOWN)
        self.play(Write(text3))
        self.wait()
        find_x9 = Tex(r"x", "=", "{", "2", "d", "\left[", "3", "\sin", "(\gamma)", "-", "4", "\sin", "^3", "(\gamma)", r"\right]", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
                      r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(transform_parts(
            [4, 8], [4, 17], find_x8, find_x9, transtype=TransformMatchingTex))
        self.wait(gap)
        find_x10 = Tex(r"x", "=", "{", "2", "d", "\sin", "(\gamma)", "\left[", "3", "-", "4", "\sin", "^2", "(\gamma)", r"\right]", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(TransformMatchingTex(find_x9, find_x10,
                  key_map={"^2": "^3"}), FadeOut(text3))
        self.wait(gap)
        find_x11 = Tex(r"x", "=", "{", "2", "d", "\sin", "(\gamma)", "\left[", "3", "-", "3", "\sin", "^2", "(\gamma)", "-", "\sin", "^2", "(\gamma)", r"\right]", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(TransformMatchingTex(find_x10, find_x11, key_map={"4": "3"}))
        self.wait(gap)
        find_x12 = Tex(r"x", "=", "{", "2", "d", "\sin", "(\gamma)", "\left[", "3", "\cos", "^2", "(\gamma)", "-", "\sin", "^2", "(\gamma)", r"\right]", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)

        self.play(transform_parts([9, 16], [9, 14], find_x11, find_x12))
        self.wait(gap)
        find_x13 = Tex(r"x", "=", "{", "2", "d", "\sin", "(\gamma)", "\left[", "\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", r"\right]", "\left[", "\sqrt{3}", "\cos", "(\gamma)", "+", "\sin", "(\gamma)", r"\right]", "\sin", r"(\beta)", "\over", r"\sqrt{3}", "\cos", "(\gamma)", "-", "\sin", "(\gamma)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        text4 = TexText("我们利用平方差公式").to_edge(DOWN)
        self.play(Write(text4))
        self.wait()
        self.play(transform_parts(
            [8, 21], [8, 31], find_x12, find_x13, transtype=TransformMatchingTex))
        self.wait(gap)
        find_x14 = Tex(r"x", "=", "{", "2", "d", "\sin", "(\gamma)", "\left[", "\sqrt{3}", "\cos", "(\gamma)", "+", "\sin", "(\gamma)", r"\right]", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "="]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        over = Cross(find_x13[8:20])[0]
        down = Cross(find_x13[37:])[0]
        self.play(ShowCreation(over), ShowCreation(down))
        self.wait()
        self.play(FadeOut(over), FadeOut(down), FadeOut(find_x13[8:20]), FadeOut(
            find_x13[36:]), )
        self.play(ReplacementTransform(find_x13[:8], find_x14[:8]), ReplacementTransform(
            find_x13[20:36], find_x14[8:]))
        self.wait(gap)
        find_x15 = Tex(r"x", "=", "{", "4", "d", "\sin", "(\gamma)", "\left[", "{\sqrt{3}\\over 2}", "\cos", "(\gamma)", "+", "{1\\over 2}", "\sin", "(\gamma)", r"\right]", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", "\\over"]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(FadeOut(text4), TransformMatchingTex(
            find_x14, find_x15, key_map={"2": "4"}))
        self.wait(gap)
        find_x16 = Tex(r"x", "=", "{", "4", "d", "\sin", "(\gamma)", "\left[", "\sin", "(60^\circ)", "\cos", "(\gamma)", "+", "\cos", "(60^\circ)", "\sin", "(\gamma)", r"\right]", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", ]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(
            ReplacementTransform(find_x15[:9], find_x16[:9]),
            ReplacementTransform(find_x15[9:12], find_x16[9:13]),
            ReplacementTransform(find_x15[12:17], find_x16[13:18]),
            ReplacementTransform(find_x15[17:20], find_x16[18:22]),
            ReplacementTransform(find_x15[20:], find_x16[22:]),
        )
        self.wait(gap)
        find_x17 = Tex(r"x", "=", "{", "4", "d", "\sin", "(\gamma)", "\sin", "(60^\circ+\gamma)", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", ]).next_to(ABF, DOWN, buff=0, coor_mask=np.array([0, 1, 1])).set_color_by_tex_to_color_map(map)
        self.play(FlashAround(Sine))
        self.wait()
        self.play(transform_parts([8, 26], [8, 13], find_x16, find_x17))
        self.wait(gap)
        x_value = find_x17[2:]
        self.play(FadeOut(VGroup(alpha, beta, alpha_label, beta_label, note, c_value, Sine, SineMinus, c, alphabetalabel, x, find_x17[:2])), anglealphabeta.animate.set_opacity(0),
                  x_value.animate.scale(.5).move_to((ABF.get_vertices()[-1]+ABF.get_vertices()[0])/2+UP/5).rotate(get_angle(ABF.get_vertices()[-1], ABF.get_vertices()[0]), about_point=(ABF.get_vertices()[-1]+ABF.get_vertices()[0])/2))
        self.wait()


class AEtoAFratio(Scene):
    def construct(self):
        tri = moly().scale(1.5)
        lines = tri.get_trisector()
        center = tri.get_equilateral()
        angle = tri.get_angles()
        angle_label = tri.get_angle_labels()
        self.add(tri, lines, center, angle, angle_label)
        A = Tex("A").next_to(tri.get_vertices()[0], UP)
        B = Tex("B").next_to(tri.get_vertices()[1], DL)
        C = Tex("C").next_to(tri.get_vertices()[2], DR)

        vg = VGroup(A, B, C)
        self.add(vg)

        DEF = VGroup(*[Tex(tex).move_to(tri.get_equilateral_vertices()[i]*1.8-sum(
            tri.get_equilateral_vertices())*0.8/3) for i, tex in enumerate(["D", "E", "F"])])
        self.add(DEF)

        angle_sum = Tex(r"\alpha+\beta+\gamma=60^\circ",
                        isolate=[r"\alpha", r"\beta", r"\gamma", "="]).next_to(tri, DOWN)
        angle_sum.set_color_by_tex_to_color_map(
            map).fix_in_frame().scale(.5).to_edge(UL)

        ABF = moly([np.array([1.5, 2.75, 0]), np.array(
            [-3, -1.75, 0]), np.array([0.37063826, 0.1960389, 0])]).fix_in_frame()
        self.add(ABF, angle_sum)
        x_value = Tex("4", "d", "\sin", "(\gamma)", "\sin", "(60^\circ+\gamma)", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", ]).set_color_by_tex_to_color_map(map).scale(.5).move_to((ABF.get_vertices()[-1]+ABF.get_vertices()[0])/2+UP/5).rotate(get_angle(ABF.get_vertices()[-1], ABF.get_vertices()[0]), about_point=(ABF.get_vertices()[-1]+ABF.get_vertices()[0])/2).fix_in_frame()
        self.add(x_value)
        frame = self.camera.frame
        frame.shift(DOWN*8)
        self.wait()
        self.add(ABF, x_value)
        self.play(LaggedStart(frame.animate.shift(UP*8), FadeOut(ABF),
                  x_value.animate.set_opacity(1), lag_ratio=.5))
        self.wait()
        text = TexText("同样地，我们可以求出$AE$的长度").to_edge(DOWN)
        临时征用两个坐标 = [tri.get_vertices()[0], tri.get_equilateral_vertices()[1]]
        # 写到这里的时候空调水滴我桌上
        y_value = Tex("4", "d", "\sin", "(\gamma)", "\sin", r"(60^\circ+\beta)", "\sin", r"(\beta)", isolate=[
            r"\alpha", r"\beta", r"\gamma", "(", ")", "-", "+", "=", ]).set_color_by_tex_to_color_map(map).scale(.5).move_to(sum(临时征用两个坐标)/2+UP/5).rotate(get_angle(临时征用两个坐标[0], 临时征用两个坐标[1]), about_point=(sum(临时征用两个坐标)/2)).fix_in_frame()
        self.play(Write(text))
        self.wait()
        self.play(Write(y_value))
        delta = Arc(stroke_width=2, arc_center=tri.get_equilateral_vertices()[-1], start_angle=get_angle(tri.get_equilateral_vertices()[-1], tri.get_equilateral_vertices()[1]), angle=get_angle(
            tri.get_equilateral_vertices()[-1], tri.get_vertices()[0])-get_angle(tri.get_equilateral_vertices()[-1], tri.get_equilateral_vertices()[1]), radius=.25, color=PURPLE_B)
        epsilon = Arc(stroke_width=2, arc_center=tri.get_equilateral_vertices()[1], start_angle=get_angle(tri.get_equilateral_vertices()[1], tri.get_vertices()[0]), angle=get_angle(
            tri.get_equilateral_vertices()[1], tri.get_equilateral_vertices()[2])-get_angle(tri.get_equilateral_vertices()[1], tri.get_vertices()[0]), radius=.25, color=PINK)
        angle_delta = Tex("\delta").scale(.5).move_to(
            get_angle_label(delta, .1)).set_color_by_tex_to_color_map(map)
        angle_epsilon = Tex("\epsilon").scale(.5).move_to(
            get_angle_label(epsilon, .1)).set_color_by_tex_to_color_map(map)
        self.play(ShowCreation(VGroup(delta)), ShowCreation(VGroup(epsilon)))
        self.wait()
        self.play(Write(angle_delta), Write(angle_epsilon))
        self.wait()
        text2 = TexText("由正弦定理我们可以知道").to_edge(DOWN, buff=0.1)
        ratio1 = Tex("{", "4", "d", "\sin", "(\gamma)", "\sin", "(60^\circ+\gamma)", "\sin", r"(\beta)", "\\over", "\sin", "(\epsilon)", "}", "=",
                     "{", "4", "d", "\sin", "(\gamma)", "\sin", r"(60^\circ+\beta)", "\sin", r"(\beta)", "\\over", "\sin", "(\delta)", "}", isolate=[r"\alpha", r"\beta", r"\gamma", "\delta", "\epsilon", "(", ")", "+", "-", "="]
                     ).scale(.5).set_color_by_tex_to_color_map(map).next_to(B, DOWN, coor_mask=np.array([0, 1, 1]))
        self.play(Write(text2), FadeOut(text))
        self.wait()
        self.play(
            ReplacementTransform(x_value, ratio1[:16]),
            ReplacementTransform(angle_epsilon[0].copy(), ratio1[19]),
            ReplacementTransform(y_value, ratio1[22:38]),
            ReplacementTransform(angle_delta[0].copy(), ratio1[41]),
            *[Write(ratio1[i]) for i in [16, 17, 18, 20, 21, 38, 39, 40, 42]]
        )
        self.wait(2)
        ratio2 = Tex("{", "\sin", "(60^\circ+\gamma)", "\\over", "\sin", "(\epsilon)", "}", "=",
                     "{", "\sin", r"(60^\circ+\beta)", "\\over", "\sin", "(\delta)", "}", isolate=[r"\alpha", r"\beta", r"\gamma", "\delta", "\epsilon", "(", ")", "+", "-", "="]
                     ).set_color_by_tex_to_color_map(map).next_to(tri, DOWN, coor_mask=np.array([0, 1, 1]))
        cross1 = Cross(ratio1[:6])[0]
        cross2 = Cross(ratio1[12:16])[0]
        cross3 = Cross(ratio1[22:28])[0]
        cross4 = Cross(ratio1[34:38])[0]
        self.play(*[ShowCreation(i) for i in [cross1, cross2, cross3, cross4]])
        self.wait()
        self.play(FadeOut(VGroup(
            ratio1[:6], ratio1[12:16], ratio1[22:28], ratio1[34:38], cross1, cross2, cross3, cross4)))
        self.wait()
        self.play(
            ReplacementTransform(ratio1[6:12], ratio2[:6]),
            ReplacementTransform(ratio1[16:22], ratio2[6:12]),
            ReplacementTransform(ratio1[28:34], ratio2[12:18]),
            ReplacementTransform(ratio1[38:], ratio2[18:]),
            FadeOut(text2)
        )
        self.wait(2)
        text3 = TexText(r"同时，观察$\triangle AEF$可以得到").to_edge(DOWN, buff=.1)
        self.play(Write(text3))
        self.wait()
        angle_sum2 = Tex(r"\alpha+\delta+\epsilon=180^\circ", isolate=[
                         "+", "="]).set_color_by_tex_to_color_map(map).next_to(ratio2, DOWN)
        self.play(
            frame.animate.shift(DOWN),
            text3.animate.shift(DOWN),
            ReplacementTransform(angle_label[0][1][0].copy(), angle_sum2[0]),
            ReplacementTransform(angle_delta[0].copy(), angle_sum2[2]),
            ReplacementTransform(angle_epsilon[0].copy(), angle_sum2[4]),
            *[Write(angle_sum2[i]) for i in [1, 3, 5, 6]]
        )
        self.wait(2)
        need_to_fade = VGroup(tri, lines, angle, angle_label, angle_delta,
                              angle_epsilon, delta, epsilon, vg, DEF, center, text3)
        self.play(FadeOut(need_to_fade))
        equations = VGroup(ratio2, angle_sum2)
        self.play(frame.animate.move_to(ORIGIN),
                  equations.animate.arrange(DOWN).move_to(ORIGIN))
        brace = Brace(equations, LEFT)
        brace.save_state()
        brace.set_opacity(0).stretch(0, 1)
        self.play(brace.animate.restore())
        self.wait()
        text4 = TexText("于是我们获得了一个关于$\delta$和$\epsilon$的方程组").to_edge(
            DOWN).insert_n_curves(30)
        self.play(Write(text4))
        self.wait(3)
        self.play(FadeOut(text4))


isolates = [r"\alpha", r"\beta", r"\gamma",
            "\delta", "\epsilon", "+", "=", "-", "(", ")"]


class solveequ(Scene):
    def construct(self):
        ratio = Tex("{", "\sin", "(60^\circ+\gamma)", "\\over", "\sin", "(\epsilon)", "}", "=",
                    "{", "\sin", r"(60^\circ+\beta)", "\\over", "\sin", "(\delta)", "}", isolate=[r"\alpha", r"\beta", r"\gamma", "\delta", "\epsilon", "(", ")", "+", "-", "="]
                    ).set_color_by_tex_to_color_map(map)
        angle_sum = Tex(r"\alpha+\delta+\epsilon=180^\circ",
                        isolate=["+", "="]).set_color_by_tex_to_color_map(map)
        equations = VGroup(ratio, angle_sum).arrange(DOWN).move_to(ORIGIN)
        brace = Brace(equations, LEFT)
        equations.add(brace)

        垃圾玩意儿 = Tex(r"\alpha+\beta+\gamma=60^\circ",
                    isolate=[r"\alpha", r"\beta", r"\gamma", "="])
        垃圾玩意儿.set_color_by_tex_to_color_map(
            map).fix_in_frame().scale(.5).to_edge(UL)

        self.add(equations, 垃圾玩意儿)
        text1 = TexText("我们很容易能找到一组解").to_edge(DOWN)
        solution = TexText(
            r"$$\begin{cases}\delta=60^\circ+\beta\\\epsilon=60^\circ+\gamma\end{cases}$$",).next_to(equations, DOWN)
        solution[0][1].set_color(map[r"\delta"])
        solution[0][7].set_color(map[r"\beta"])
        solution[0][8].set_color(map[r"\epsilon"])
        solution[0][14].set_color(map[r"\gamma"])
        self.play(Write(text1))
        self.wait()
        self.play(Write(solution))
        self.wait(2)
        临时箭头 = CurvedArrow(solution.get_right()[
                           1]*UP+RIGHT*3, equations.get_right()[1]*UP+RIGHT*3, angle=PI*.9, color=GREY)
        临时tip = TexText("自己代进去试试看！").scale(.5).set_color(GREY).next_to(临时箭头)
        self.play(ShowCreation(VGroup(临时箭头)), Write(临时tip))
        self.wait(2)
        self.play(Uncreate(临时箭头), FadeOut(临时tip))
        text2 = TexText("会不会有其他答案呢？").to_edge(DOWN)
        self.wait()
        self.play(FadeOut(text1), Write(text2))
        self.wait(2)
        text3 = TexText("让我们来探究一下").to_edge(DOWN)
        self.play(FadeOut(text2), Write(text3))
        self.wait(2)
        self.play(FadeOut(text3), solution.animate.scale(.5).next_to(
            垃圾玩意儿, DOWN).align_to(垃圾玩意儿, LEFT), equations.animate.scale(.8).to_edge(UP))
        text4 = TexText("我们可以把这两个式子画在一个平面上").to_edge(DOWN)
        self.wait()
        self.play(Write(text4))

        delta_line = VGroup(
            Line(np.array([-PI/2, -PI/2-1, 0]),
                 np.array([PI/2, -PI/2-1, 0])).set_color(map[r"\delta"]).set_stroke(width=2),
            Tex("0^\circ").scale(.3).next_to(
                np.array([-PI/2, -PI/2-1, 0]), LEFT).set_color(PURPLE_A),
            Tex("180^\circ").scale(.3).next_to(
                np.array([PI/2, -PI/2-1, 0])).set_color(PURPLE_A),
            Tex(r"\delta").next_to(
                np.array([0, -PI/2-1, 0]), UP).set_color(PURPLE_B),
        )
        epsilon_line = VGroup(
            Line(np.array([-PI/2-1, -PI/2, 0]), np.array([-PI /
                 2-1, PI/2, 0])).set_color(map[r"\epsilon"]).set_stroke(width=2),
            Tex("0^\circ").scale(.3).next_to(
                np.array([-PI/2-1, -PI/2, 0]), DOWN).set_color(LIGHT_PINK),
            Tex("180^\circ").scale(.3).next_to(
                np.array([-PI/2-1, PI/2, 0]), UP).set_color(LIGHT_PINK),
            Tex(r"\epsilon").next_to(
                np.array([-PI/2-1, 0, 0])).set_color(PINK),
        )
        self.play(ShowCreation(VGroup(delta_line[0])), ShowCreation(
            VGroup((epsilon_line[0]))))
        self.play(Write(delta_line[1:]), Write(epsilon_line[1:]))
        self.wait()
        horizon = VGroup(*[delta_line[0].copy().move_to(i*UP)
                         for i in np.arange(-PI/2, PI/2+.1, PI/6)])
        vertic = VGroup(*[epsilon_line[0].copy().move_to(i*RIGHT)
                        for i in np.arange(-PI/2, PI/2+.1, PI/6)])
        self.play(
            *[ReplacementTransform(delta_line[0].copy(), line)
              for line in horizon],
            *[ReplacementTransform(epsilon_line[0].copy(), line) for line in vertic]
        )
        self.wait(2)

        beta_slider = VGroup(
            Line(np.array([2.5, 1, 0]), np.array(
                [2.5, -1, 0])).set_stroke(width=2),
            Dot(np.array([2.5, np.cos(1)*0.8-0.2, 0])
                ).set_color(map[r"\beta"]),
            Tex(r"\beta").next_to(2.5*RIGHT+(np.cos(1)*0.8-0.2)
                                  * UP, LEFT).set_color_by_tex_to_color_map(map),
            Tex("0^\circ").scale(.3).next_to(np.array([2.5, -1, 0]), DOWN),
            Tex("60^\circ").scale(.3).next_to(np.array([2.5, 1, 0]), UP)
        ).shift(RIGHT)

        gamma_slider = VGroup(
            Line(np.array([2.5, 1, 0]), np.array(
                [2.5, -1, 0])).set_stroke(width=2),
            Dot(np.array([2.5, -1, 0])).set_color(map[r"\gamma"]),
            Tex(r"\gamma").next_to(
                np.array([2.5, -1, 0]), LEFT).set_color_by_tex_to_color_map(map),
            Tex("0^\circ").scale(.3).next_to(np.array([2.5, -1, 0]), DOWN),
            Tex("60^\circ").scale(.3).next_to(np.array([2.5, 1, 0]), UP)
        ).shift(RIGHT*2)

        self.play(ShowCreation(beta_slider[:1]),
                  ShowCreation(gamma_slider[:1]))
        self.play(Write(beta_slider[1]), Write(gamma_slider[1]))
        self.play(Write(beta_slider[2:]), Write(gamma_slider[2:]))
        self.wait(2)

        value = ValueTracker(value=0)

        def func(a, b):
            if np.sin(PI/2)*np.sin(a+PI/3)/np.sin(b+PI/3) < 1:
                def f(x): return np.arcsin(
                    np.sin(x)*np.sin(a+PI/3)/np.sin(b+PI/3))
                return VGroup(FunctionGraph(lambda x: f(x), x_range=[0, PI, PI/100]),
                              FunctionGraph(lambda x: f(x), x_range=[0, PI, PI/100]).rotate(-PI, about_point=np.array([PI/2, PI/2, 0]))).set_color(LIGHT_PINK)
            else:
                def f(x): return np.arcsin(
                    np.sin(x)*np.sin(b+PI/3)/np.sin(a+PI/3))
                return VGroup(FunctionGraph(lambda x: f(x), x_range=[0, PI, PI/100]).rotate(-3*PI/2, about_point=np.array([PI/2, PI/2, 0])),
                              FunctionGraph(lambda x: f(x), x_range=[0, PI, PI/100]).rotate(-PI/2, about_point=np.array([PI/2, PI/2, 0]))).set_color(LIGHT_PINK)

        two_arms = func(0, (np.cos(1)*0.8-0.2+1)/2*PI/3).center()
        line = Line((2*PI/3+(np.cos(1)*0.8-0.2+1)/2*PI/3)*UP, (2*PI/3 +
                    (np.cos(1)*0.8-0.2+1)/2*PI/3)*RIGHT).shift(DL*PI/2).set_color(PURPLE_A)
        intersect = Dot(
            np.array([PI/3+(np.cos(1)*0.8-0.2+1)/2*PI/3-PI/2, PI/3-PI/2, 0]))
        self.play(FlashAround(ratio), FadeOut(text4))
        self.wait()
        self.play(ShowCreation(two_arms))
        self.wait()
        self.play(FlashAround(angle_sum))
        self.wait()
        self.play(ShowCreation(VGroup(line)))
        self.wait()
        self.play(Write(intersect))

        def update_value(obj, dt):
            obj.increment_value(dt)

        def update_beta_dot(obj):
            obj.move_to(
                np.array([3.5, np.cos(value.get_value()+1)*0.8-0.2, 0]))

        def update_gamma_dot(obj):
            obj.move_to(np.array([4.5, -np.cos(value.get_value())*0.5-0.5, 0]))

        def update_beta_label(obj):
            obj.next_to(
                np.array([3.5, np.cos(value.get_value()+1)*0.8-0.2, 0]), LEFT)

        def update_gamma_label(obj):
            obj.next_to(
                np.array([4.5, -np.cos(value.get_value())*0.5-0.5, 0]), LEFT)

        def update_arms(obj):
            obj.become(func((-np.cos(value.get_value())*0.5+0.5)*PI/6,
                       (np.cos(value.get_value()+1)*0.8+0.8)*PI/6)).center()

        def update_line(obj):
            obj.put_start_and_end_on(
                (2*PI/3 + (-np.cos(value.get_value())*0.5+0.5)*PI /
                 6 + (np.cos(value.get_value()+1)*0.8+0.8)*PI/6) * UP,
                (2*PI/3 + (-np.cos(value.get_value())*0.5+0.5)*PI/6 + (np.cos(value.get_value()+1)*0.8+0.8)*PI/6)*RIGHT,)
            obj.shift(DL*PI/2)

        def update_intersect(obj):
            obj.move_to(np.array([-PI/6+(np.cos(value.get_value()+1)*0.8-0.2+1)
                        * PI/6, -PI/6+(-np.cos(value.get_value())*0.5+0.5)*PI/6, 0]))

        value.add_updater(update_value)
        beta_slider[1].add_updater(update_beta_dot)
        beta_slider[2].add_updater(update_beta_label)
        gamma_slider[1].add_updater(update_gamma_dot)
        gamma_slider[2].add_updater(update_gamma_label)
        two_arms.add_updater(update_arms)
        line.add_updater(update_line)
        intersect.add_updater(update_intersect)
        self.add(value, *beta_slider[1:3], *gamma_slider[1:3], line, intersect)
        self.wait(5)

        that_direction = Arrow(
            PI/2*DL, PI/2*UR, buff=0).set_color(BLUE).shift(DR)
        self.play(GrowArrow(that_direction))
        self.wait()
        text6 = TexText("我们可以发现").to_edge(DOWN)
        text7 = TexText("无论这条直线在这个方向上的任何位置").to_edge(DOWN)
        text8 = TexText("都只会有一个交点").to_edge(DOWN)
        self.play(Write(text6))
        self.wait()
        self.play(line.animate.set_color(YELLOW))
        self.wait()
        self.play(FadeOut(text6), Write(text7))
        self.play(Indicate(that_direction))
        self.wait(2)
        self.play(FadeOut(text7), Write(text8))
        self.play(Indicate(intersect))
        self.wait(2)
        text9 = TexText("也就是说，方程组只有一组“有意义”的解").to_edge(
            DOWN).insert_n_curves(40)
        self.play(FadeOut(text8), Write(text9), FadeOut(
            that_direction), line.animate.set_color(PURPLE_A))
        self.wait()
        self.play(FlashAround(solution))
        self.wait()
        self.play(FadeOut(text9))
        self.wait()
        value.clear_updaters()

        beta_slider[1].clear_updaters()
        beta_slider[2].clear_updaters()
        gamma_slider[1].clear_updaters()
        gamma_slider[2].clear_updaters()

        two_arms.clear_updaters()
        line.clear_updaters()
        intersect.clear_updaters()
        self.play(FadeOut(VGroup(beta_slider, gamma_slider, horizon, vertic,
                  two_arms, line, intersect, equations, delta_line, epsilon_line)))
        self.wait()


def angle_long_label(tex, angle, buff):
    tex.move_to(get_angle_label(angle, buff))
    tex.rotate(get_angle(angle.arc_center, angle.pfp(0.5)) % PI-PI/2)


def angle_arc(p1, p2, p3, radius=.25):
    start = get_angle(p2, p1)
    end = get_angle(p2, p3)
    if start > end:
        end += PI*2
    return Arc(start, end-start, radius=radius, arc_center=p2, stroke_width=2)


class final(Scene):
    def construct(self):

        solution = TexText(
            r"$$\begin{cases}\delta=60^\circ+\beta\\\epsilon=60^\circ+\gamma\end{cases}$$",)
        solution[0][1].set_color(map[r"\delta"])
        solution[0][7].set_color(map[r"\beta"])
        solution[0][8].set_color(map[r"\epsilon"])
        solution[0][14].set_color(map[r"\gamma"])
        angle_sum = Tex(r"\alpha+\beta+\gamma=60^\circ",
                        isolate=[r"\alpha", r"\beta", r"\gamma", "="])
        angle_sum.set_color_by_tex_to_color_map(
            map).scale(.5).to_edge(UL)
        self.add(angle_sum)
        solution.scale(.5).next_to(angle_sum, DOWN).align_to(angle_sum, LEFT)
        self.add(solution)
        self.wait()
        tri = moly().scale(1.5)
        lines = tri.get_trisector()
        center = tri.get_equilateral()
        angle = tri.get_angles()
        angle_label = tri.get_angle_labels()
        self.play(FadeIn(VGroup(tri, lines, center, angle, angle_label)))
        A = Tex("A").next_to(tri.get_vertices()[0], UP)
        B = Tex("B").next_to(tri.get_vertices()[1], DL)
        C = Tex("C").next_to(tri.get_vertices()[2], DR)

        vg = VGroup(A, B, C)
        self.play(FadeIn(vg))

        DEF = VGroup(*[Tex(tex).move_to(tri.get_equilateral_vertices()[i]*1.8-sum(
            tri.get_equilateral_vertices())*0.8/3) for i, tex in enumerate(["D", "E", "F"])])
        self.play(FadeIn(DEF))

        delta = Arc(
            stroke_width=2,
            arc_center=tri.get_equilateral_vertices()[-1],
            start_angle=get_angle(tri.get_equilateral_vertices(
            )[-1], tri.get_equilateral_vertices()[1]),
            angle=get_angle(tri.get_equilateral_vertices()[-1], tri.get_vertices()[0])-get_angle(
                tri.get_equilateral_vertices()[-1], tri.get_equilateral_vertices()[1]),
            radius=.25,
            color=PURPLE_B
        )
        epsilon = Arc(
            stroke_width=2,
            arc_center=tri.get_equilateral_vertices()[1],
            start_angle=get_angle(tri.get_equilateral_vertices()[
                                  1], tri.get_vertices()[0]),
            angle=get_angle(tri.get_equilateral_vertices()[1], tri.get_equilateral_vertices()[
                            2])-get_angle(tri.get_equilateral_vertices()[1], tri.get_vertices()[0]),
            radius=.25,
            color=PINK
        )
        angle_delta = Tex("\delta").scale(.5).move_to(
            get_angle_label(delta, .1)).set_color_by_tex_to_color_map(map)
        angle_epsilon = Tex("\epsilon").scale(.5).move_to(
            get_angle_label(epsilon, .1)).set_color_by_tex_to_color_map(map)
        self.play(FadeIn(VGroup(delta, epsilon, angle_delta, angle_epsilon)))
        self.wait()
        self.play(solution.animate.scale(2).next_to(tri, DOWN))
        self.wait()
        for_delta = Tex("6", "0", "^\circ", "+",
                        r"\beta").set_color_by_tex_to_color_map(map).scale(.3)
        for_epsilon = Tex("6", "0", "^\circ", "+",
                          r"\gamma").set_color_by_tex_to_color_map(map).scale(.3)
        angle_long_label(for_delta, delta, 0.1)
        angle_long_label(for_epsilon, epsilon, 0.1)
        self.play(FadeOut(angle_delta), ReplacementTransform(
            solution[0][3:8].copy(), for_delta[:]), delta.animate.set_color(YELLOW_B))
        self.play(FadeOut(angle_epsilon), ReplacementTransform(
            solution[0][10:].copy(), for_epsilon[:]), epsilon.animate.set_color(TEAL_B))
        self.wait()
        self.play(FadeOut(solution))
        self.wait()
        # 各点坐标:

        Ar = tri.get_vertices()[0]
        Be = tri.get_vertices()[1]
        Ca = tri.get_vertices()[2]
        Db = tri.get_equilateral_vertices()[0]
        Ee = tri.get_equilateral_vertices()[1]  # 嘿嘿，我这个找不到对应的元素
        Fe = tri.get_equilateral_vertices()[2]

        angle_BFD = angle_arc(Be, Fe, Db).set_color(RED_B)
        for_BFD = Tex("60^\circ", "+",
                      "\\alpha").set_color_by_tex_to_color_map(map).scale(.3)
        angle_long_label(for_BFD, angle_BFD, buff=.1)
        angle_BDF = angle_arc(Fe, Db, Be).set_color(TEAL_B)
        for_BDF = Tex("60^\circ", "+",
                      "\\gamma").set_color_by_tex_to_color_map(map).scale(.3)
        angle_long_label(for_BDF, angle_BDF, buff=.1)
        angle_CDE = angle_arc(Ca, Db, Ee).set_color(YELLOW_B)
        for_CDE = Tex("60^\circ", "+",
                      "\\beta").set_color_by_tex_to_color_map(map).scale(.3)
        angle_long_label(for_CDE, angle_CDE, buff=.1)
        angle_CED = angle_arc(Db, Ee, Ca).set_color(RED_B)
        for_CED = Tex("60^\circ", "+",
                      "\\alpha").set_color_by_tex_to_color_map(map).scale(.3)
        angle_long_label(for_CED, angle_CED, buff=.1)

        text = TexText("同理，我们可以求出剩余角的角度").to_edge(DOWN)
        self.play(Write(text))
        self.wait()
        self.play(LaggedStart(*[ShowCreation(VGroup(i))
                  for i in [angle_BFD, angle_BDF, angle_CDE, angle_CED]], lag_ratio=.2))
        self.wait()
        self.play(LaggedStart(
            *[Write(i) for i in [for_BFD, for_BDF, for_CDE, for_CED]], lag_ratio=.2))
        self.wait()

        angle_AFB = angle_arc(Ar, Fe, Be).set_color([RED_B, YELLOW_B])
        for_AFB = Tex("180^\circ", "-", "\\alpha", "-",
                      "\\beta").set_color_by_tex_to_color_map(map).scale(.5)
        angle_long_label(for_AFB, angle_AFB, buff=.1)
        angle_BDC = angle_arc(Be, Db, Ca).set_color([YELLOW_B, TEAL_B])
        for_BDC = Tex("180^\circ", "-", "\\beta", "-",
                      "\\gamma").set_color_by_tex_to_color_map(map).scale(.5)
        angle_long_label(for_BDC, angle_BDC, buff=.1)
        angle_CEA = angle_arc(Ca, Ee, Ar).set_color([TEAL_B, RED_B])
        for_CEA = Tex("180^\circ", "-", "\\gamma", "-",
                      "\\alpha").set_color_by_tex_to_color_map(map).scale(.5)
        angle_long_label(for_CEA, angle_CEA, buff=.1)

        text2 = TexText(
            r"同时我们可以求出$\angle AFB,\angle BDC,\angle CEA$的大小").to_edge(DOWN)
        self.play(FadeOut(text), Write(text2))
        self.wait()
        self.play(LaggedStart(*[ShowCreation(VGroup(i))
                  for i in [angle_AFB, angle_BDC, angle_CEA]], lag_ratio=.2))
        self.wait()
        self.play(DEF.animate.set_opacity(.1))
        self.play(
            LaggedStart(
                *[
                    AnimationGroup(Write(i[0]), Write(i[1]), Write(i[3]),
                                   ReplacementTransform(j[0].copy(), i[2]), ReplacementTransform(k[0].copy(), i[4]))
                    for i, j, k in zip(
                        [for_AFB, for_BDC, for_CEA],
                        [i[2] for i in angle_label],
                        [j[0] for j in [angle_label[1], angle_label[2], angle_label[0]]]
                    )
                ], lag_ratio=.2
            )
        )
        self.wait()
        remain = moly(vertices=tri.get_equilateral_vertices()
                      ).get_big_angles().set_color(ORANGE)
        texs = VGroup(
            *[Tex("60^\circ").scale(.3).move_to(get_angle_label(arc, buff=.1)) for arc in remain])
        text3 = TexText("通过最后一点简单的计算可得").to_edge(DOWN)
        self.play(Write(text3), FadeOut(text2))
        self.wait()
        self.play(*[ShowCreation(VGroup(arc)) for arc in remain])
        self.wait()
        self.play(*[Write(tex) for tex in texs])
        self.wait()
        text4 = TexText(r"所以$\triangle DEF$是正三角形！").to_edge(
            DOWN).insert_n_curves(30)
        self.play(Write(text4), FadeOut(text3))
        self.wait(3)
        self.play(FadeOut(text4))
        self.play(FadeOut(
            VGroup(
                angle_sum,
                angle_label, texs, remain, A, B, C, DEF, angle,
                angle_BFD, angle_BDF, angle_CDE, angle_CED, delta, epsilon,
                for_delta, for_epsilon, for_AFB, for_BDC, for_CEA, for_BFD, for_BDF, for_CDE, for_CED,
            )
        ),
            angle_AFB.animate.set_opacity(0),
            angle_BDC.animate.set_opacity(0),
            angle_CEA.animate.set_opacity(0),
        )
        self.wait()


def rdarray(x_min,x_max,y_min,y_max):
    return np.array([random.uniform(x_min,x_max),random.uniform(y_min,y_max),0])


class Random(Scene):
    def construct(self):
        tri = moly().scale(1.5)
        print(tri.get_vertices())
        line = tri.get_trisector()
        center = tri.get_equilateral()
        value = ValueTracker()

        def update_value(obj, dt):
            obj.increment_value(dt)

        def update_tri(obj):
            t = value.get_value()
            A = np.array([1.25*np.cos(2*t+PI/2)+1.5,
                         1.25*np.sin(2*t+PI/2)+2, 0])
            B = np.array([np.cos(-3/2*t+PI)-2, np.sin(-3/2*t+PI)-1.75, 0])
            C = np.array([1.25*np.cos(5/2*t)+1.75, 1.25*np.sin(5/2*t)-1.75, 0])
            obj.become(moly([A, B, C]))

        def update_line(obj):
            obj.become(tri.get_trisector())

        def update_center(obj):
            obj.become(tri.get_equilateral())
        value.add_updater(update_value)
        tri.add_updater(update_tri)
        line.add_updater(update_line)
        center.add_updater(update_center)
        self.add(value, tri, line, center)
        self.wait(14)


class thumbnail(Scene):
    def construct(self):
        tri = moly().scale(1.5)
        line = tri.get_trisector()
        center = tri.get_equilateral()
        self.add(tri, line, center)
        text = TexText("莫利定理").insert_n_curves(20).scale(2).next_to(tri, DOWN)
        self.add(text)
